package com.zmn.plat.admin.controller.servcategory;

import com.aliyun.openservices.shade.com.google.common.base.Joiner;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.admin.controller.PlatBaseController;
import com.zmn.plat.common.util.StringToArrayUtil;
import com.zmn.plat.model.entity.base.fault.Fault;
import com.zmn.plat.model.entity.servcategory.CategoryFaultImage;
import com.zmn.plat.services.interfaces.base.fault.FaultService;
import com.zmn.plat.services.interfaces.servcategory.CategoryFaultImageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 后端产品分类-故障图片基础修改Controller
 *
 * @author yangshuangping
 * @since 2021/11/18 18:37
 */
@Slf4j
@RestController
@RequestMapping("/category/faultimage")
public class CategoryFaultImageModifyController extends PlatBaseController {

    @Resource
    private CategoryFaultImageService categoryFaultImageService;

    @Resource
    private FaultService faultService;

    /**
     * 描述: 保存
     *
     * @param categoryFaultImage 修改实体
     * @return com.zmn.common.dto2.ResponseDTO
     * @author yangshuangping
     * @since 2021/11/18 18:37
     */
    @RequestMapping("/addEdit")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody CategoryFaultImage categoryFaultImage) {

        Date now = DateUtil.getNow();
        Integer faultId = categoryFaultImage.getFaultId();
        try {
            categoryFaultImage.setUpdater(getMcStaffRealName());
            categoryFaultImage.setUpdateTime(now);
            categoryFaultImage.setFaultRelevance(transitionFault(categoryFaultImage.getFaultRelevance()));
            if (NumberUtil.isNullOrZero(faultId)) {
                categoryFaultImage.setCreater(getMcStaffRealName());
                categoryFaultImage.setCreateTime(now);
                categoryFaultImageService.insert(categoryFaultImage);
            } else {
                categoryFaultImageService.updateByKey(categoryFaultImage);
            }
            return ResponseDTO.success("操作成功！");
        } catch (Exception e) {
            log.error("新增/修改失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 描述: 删除
     *
     * @param ids 主键
     * @return com.zmn.common.dto2.ResponseDTO<T>
     * @author yangshuangping
     * @since 2021/11/18 18:37
     */
    @RequestMapping(value = "/delete")
    public ResponseDTO delete(@RequestParam(value = "ids") String ids) {

        try {
            if (StringUtil.isBlank(ids)) {
                return ResponseDTO.fail("请选择要删除的数据！");
            }
            List<Integer> arrayIds = StringUtil.convertStrsToList(ids);
            categoryFaultImageService.deleteByKeys(arrayIds);
            return ResponseDTO.success(true, "删除成功！");
        } catch (Exception e) {
            log.error("删除失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

    private String transitionFault(String faultRelevance) {

        if (StringUtil.isBlank(faultRelevance)) {
            return null;
        }
        List<Integer> ids = StringToArrayUtil.convertStrsToList(faultRelevance);
        final List<Fault> faultList = faultService.listByIds(ids);
        if (CollectionUtil.isNullOrEmpty(faultList)) {
            return null;
        }
        final List<Integer> collect = faultList.stream()
                .map(Fault::getFaultId).collect(Collectors.toList());
        final String faulId = Joiner.on(",").join(collect);
        return faulId;
    }
}
